#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int counta = 1;
    int countb = 1;
    struct ListNode* pweia = headA;
    struct ListNode* pweib = headB;

    while (pweia->next)
    {
        counta++;
        pweia = pweia->next;
    }
    while (pweib->next)
    {
        countb++;
        pweib = pweib->next;
    }
    if (pweia != pweib)
        return NULL;

    struct ListNode* pcura = headA;
    struct ListNode* pcurb = headB;
    int k = countb - counta;
    if (k < 0)
    {
        while (k++)
        {
            pcura = pcura->next;
        }
    }
    else
    {
        while (k--)
        {
            pcurb = pcurb->next;
        }
    }
    while (pcura != pcurb)
    {
        pcura = pcura->next;
        pcurb = pcurb->next;
    }
    return pcura;
}